//
//  SKWelcomeViewController.swift
//  WeiboProjectYL
//
//  Created by yanli on 16/7/12.
//  Copyright © 2016年 mountainPassCell. All rights reserved.
//

import UIKit
import SDWebImage


class SKWelcomeViewController: UIViewController {

    // MARK: -- 懒加载控件
    private lazy var bgImageView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named:"ad_background"))
        
        return imageView
    }()
    
    //头像
    private lazy var headImageView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        // 指定大小 在布局那里设定
        
        // 判断头像地址是否为 nil
        if let imageURL = SKUserAccountViewModel.shareUserAccount.userAccount?.avatar_large {
            // 有头像地址
            imageView.sd_setImageWithURL(NSURL(string: imageURL), placeholderImage: UIImage(named: "avatar_default_big"))
        }
        
        //设置圆角
        imageView.layer.cornerRadius = 42.5
        imageView.layer.masksToBounds = true
        
        return imageView
    }()
    
    
    //消息(头像下面的文字)
    private lazy var messageLable: UILabel = {
        //创建 label 并设置
        let label = UILabel()
        label.font = UIFont.systemFontOfSize(15)
        label.textColor = UIColor.grayColor()
        
        // 判断 如果名字是解档出来的对象的名字
        if let name = SKUserAccountViewModel.shareUserAccount.userAccount?.name {
            label.text = "欢迎回来,\(name)"
        }else {
            label.text = "欢迎回来"
        }
        
        return label
    }()
    
    //重写自定义视图 view
    override func loadView() {
        // 自定义视图
        view = bgImageView
    }
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        //添加视图设置约束
        setupUI()
    }

    
    //view 将要呈现
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        //执行动画
        startAnimation()
    }
    

    // 添加视图设置约束
    private func setupUI() {
        // 设置约束之前 先要添加到主视图中
        view.addSubview(headImageView)
        view.addSubview(messageLable)
        
        //布局 设置约束
        //头像 布局
        headImageView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(view).offset(200)
            make.centerX.equalTo(view)
            make.size.equalTo(CGSize(width: 85, height: 85))
        }
        // 消息 布局
        messageLable.snp_makeConstraints { (make) -> Void in
            // 相对于头像
            make.top.equalTo(headImageView.snp_bottom).offset(10)
            make.centerX.equalTo(headImageView)
        }

    }
    
    
    // 开启动画
    private func startAnimation() {
        //更新 布局
        self.headImageView.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(self.view).offset(100)
        }
        
        //usingSpringWithDamping 阻尼, 取值范围是0-1,阻尼越大弹簧效果越小
        //initialSpringVelocity 初始速度
        //枚举不想传入直接输入[]
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            
            // 执行约束动画 对 layout 重新 布局
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in    //尾随闭包
                
                //进入首页的动画
                UIView.animateWithDuration(1, animations: { () -> Void in
                        // 设置透明度显示
                        self.messageLable.alpha = 1
                    
                    }, completion: { (_) -> Void in
                        // 进入首页 发送通知
                        NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootVCNotification, object: nil)
                })
          }
        
    }
    
    
    
    
}















